package com.wyz.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.wyz.mapper.GoodsMapper;
import com.wyz.model.Goods;
import com.wyz.model.Supplier;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.Map;

/**
 * @author wk
 * @desc
 * @date 2024/7/26 15:26
 */
@Controller
public class GoodsController {
    @Resource
    private GoodsMapper goodsMapper;
    @RequestMapping("/goods/getAllGoods")
    public String getAllGoods(HttpSession session){
        List<Map<String, Object>> goods =goodsMapper.selectGoodsWithType();
        session.setAttribute("goods",goods);
        return "back/goods";
    }
    @RequestMapping("/goods/getGoodsById")
    public String getGoodsById(Integer id,HttpSession session){
        List<Map<String, Object>> goods = goodsMapper.getGoodsById(id);
        System.out.println(goods);
        session.setAttribute("goods",goods);
        return "back/goodsView";
    }
    @RequestMapping("/goods/addGoods")
    public String goodsAdd() {
        return "back/addgoods";
    }
    @RequestMapping("/goods/add.do")
    public String goodsAdd(Goods goods,HttpSession session){
        goodsMapper.insert(goods);
        return "redirect:/goods/getAllGoods";
    }
    @RequestMapping("/goods/edit.do")
    public String goodsEdit(Integer id,HttpSession session){
        Goods goods = goodsMapper.selectById(id);
        session.setAttribute("goods",goods);
        return "back/editgoods";
    }
    @RequestMapping("/goods/editHandler.do")
    public String goodsEditHandler(Goods goods,HttpSession session){
        int result=goodsMapper.updateById(goods);
        if (result == 0) {
            session.setAttribute("error", "修改失败"); // 修改失败后将错误信息存入session中
            return "back/editgoods";
        } else {
            session.removeAttribute("error"); // 修改成功后清除session中错误信息
            return "redirect:/goods/getAllGoods"; // 修改成功后重定向跳转到登录页面
        }

    }
    @RequestMapping("/goods/deleteGoods")
    public String goodsDel(Integer id){
        goodsMapper.deleteById(id);
        return "redirect:/goods/getAllGoods";
    }
    @RequestMapping("/goods/search.do")
    public String search(String condition,String sort,HttpSession session){
        List<Map<String, Object>> goods =goodsMapper.selectGoodsWithGoodsTypeLikeCondition(condition,sort);
        session.setAttribute("goods",goods);
        return "back/goods";
    }
}
